# Contributor: Leonardo Arena <rnalrd@alpinelinux.org>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=cyrus-sasl
pkgver=2.1.27
pkgrel=7
pkgdesc="Cyrus Simple Authentication Service Layer (SASL)"
url="https://www.cyrusimap.org/sasl/"
arch="all"
license="custom"
options="!check"  # No test suite.
subpackages="
	$pkgname-dev
	$pkgname-doc
	$pkgname-openrc
	libsasl
	$pkgname-gssapiv2:_plugin
	$pkgname-gs2:_plugin
	$pkgname-scram:_plugin
	$pkgname-ntlm:_plugin
	$pkgname-crammd5:_plugin
	$pkgname-digestmd5:_plugin
	$pkgname-login:_plugin
	"
# use heimdal to avoid circular dep: cyrus-sasl -> krb5 -> openldap -> cyrus-sasl
makedepends="
	db-dev
	openssl-dev
	heimdal-dev
	py3-sphinx

	automake
	autoconf
	libtool
	"
source="https://github.com/cyrusimap/cyrus-sasl/releases/download/cyrus-sasl-$pkgver/cyrus-sasl-$pkgver.tar.gz
	cyrus-sasl-2.1.27-as_needed.patch
	cyrus-sasl-2.1.27-autotools_fixes.patch
	cyrus-sasl-2.1.27-avoid_pic_overwrite.patch
	cyrus-sasl-2.1.27-doc_build_fix.patch
	cyrus-sasl-2.1.27-gss_c_nt_hostbased_service.patch
	CVE-2019-19906.patch
	saslauthd.initd
	"

# secfixes:
#   2.1.27-r5:
#     - CVE-2019-19906
#   2.1.26-r7:
#     - CVE-2013-4122


build() {
	autoreconf -vif
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--mandir=/usr/share/man \
		--enable-static \
		--enable-shared \
		--disable-java \
		--with-plugindir=/usr/lib/sasl2 \
		--with-configdir=/etc/sasl2 \
		--with-dbpath=/etc/sasl2/sasldb2 \
		--disable-krb4 \
		--with-gss_impl=heimdal \
		--enable-gssapi \
		--with-rc4 \
		--with-dblib=berkeley \
		--with-saslauthd=/run/saslauthd \
		--without-pwcheck \
		--with-devrandom=/dev/urandom \
		--enable-anon \
		--enable-cram \
		--enable-digest \
		--enable-ntlm \
		--enable-plain \
		--enable-login \
		--enable-auth-sasldb \
		--enable-alwaystrue \
		--disable-otp
	make
}

package() {
	make -j1 DESTDIR="$pkgdir" install
	mkdir -p "$pkgdir"/etc/sasl2
	install -D -m644 COPYING "$pkgdir"/usr/share/licenses/$pkgname/COPYING
	install -Dm755 "$srcdir"/saslauthd.initd "$pkgdir"/etc/init.d/saslauthd
}

static() {
	pkgdesc="Static libraires for $pkgname"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/*.a "$subpkgdir"/usr/lib/
}

_plugin() {
	depends=
	local plugin=${subpkgname/$pkgname-/}
	replaces="libsasl"
	pkgdesc="Cyrus SASL plugin for $plugin"
	mkdir -p "$subpkgdir"/usr/lib/sasl2
	mv "$pkgdir"/usr/lib/sasl2/lib$plugin.so* "$subpkgdir"/usr/lib/sasl2/
}

libsasl() {
	depends=
	pkgdesc="Cyrus Simple Authentication and Security Layer (SASL) library"
	replaces="cyrus-sasl-plain"

	mkdir -p "$subpkgdir"/usr/lib/sasl2 "$subpkgdir"/etc/sasl2
	local lib=
	for lib in anonymous sasldb plain; do
		mv "$pkgdir"/usr/lib/sasl2/*${lib}*.so* "$subpkgdir"/usr/lib/sasl2/
	done
	mv "$pkgdir"/usr/lib/libsasl*.so.* "$subpkgdir"/usr/lib/
}

sha512sums="d11549a99b3b06af79fc62d5478dba3305d7e7cc0824f4b91f0d2638daafbe940623eab235f85af9be38dcf5d42fc131db531c177040a85187aee5096b8df63b  cyrus-sasl-2.1.27.tar.gz
9eefa6d45e3dd9157a5672909acdd88f0ae35e76d64c3723890a474bbb05b22499cfadb0c077924d27f34da3710b2b700094dd7d5704050138c08dabcefdde94  cyrus-sasl-2.1.27-as_needed.patch
0d99ca049e76c11500769079d94f3bdb634bddb4c8d45a83b383e9bb9777edda66b17566800acbd450e1f4842d070ec3fbc236e7f0ef8759c36e6dd5ea8e3c64  cyrus-sasl-2.1.27-autotools_fixes.patch
4ca601839b023ef790e48dae567ffbbd57c632384c980946639ec7437ad23874961451718569455e6e25afaeff1728ecbc71a8686f6b43246f83465f95a2c904  cyrus-sasl-2.1.27-avoid_pic_overwrite.patch
6d723e7ec2c431b45c011b887187b6a670dbe646aa4c39d38171047ab23db529c30c433f8d4dd624181917c5ce4e5271f86e35e2644ede1c40dfb09cb67dccde  cyrus-sasl-2.1.27-doc_build_fix.patch
fca4f2b7e427c7613f71daa4a31772c33c8c0fe9d7f85b57b85da71bc5a88a18fc52f7caea463188b4addd31cd041d5349af689d5face2cc45fb50c700a8afd7  cyrus-sasl-2.1.27-gss_c_nt_hostbased_service.patch
c39efd87dc9c883d3b07474197f6835fbd32f23baa1f5cd04b25a0473639f847321c40f232e390d4dc9d9ee189dbd177c05d3d1461af4d28a48a4827abc5d9b8  CVE-2019-19906.patch
f76bfb61567172428cdbc1ed900d5e0b6e66afc38118db6ba0e2fd8ba01956ad896e56463b2249bdc46d8725384f1b975a2af3601c0735327d3f8bc26ce1ed75  saslauthd.initd"
